<?php

namespace Home\Model;


use Common\Library\BusinessException;
use Common\Library\Constant;
use Think\Model;

class CustomerContactModel extends Model {

    /**
     * 检查联系人是否存在，如果存在返回该记录，否则返回false
     * @param $customerId
     * @param $contactPhone
     * @return bool|mixed
     */
    public function checkContactExists($customerId, $contactPhone)
    {
        $contact = $this->where(['customer_id'=>$customerId, 'contact_phone'=>$contactPhone])->find();
        if ($contact) {
            return $contact;
        }
        return false;
    }

    public function has($data, $throwException = false) {
        if (is_numeric($data)) {
            $data = ['id' => $data];
        }

        if (!is_array($data)) {
            if ($throwException) {
                BusinessException::throwException(Constant::INVALID_PARAMETER);
            }
            return null;
        }

        $information = $this->where($data)->find();

        if (!$information) {
            $throwException && BusinessException::throwException(Constant::CONTACT_NOT_FOUND);
        }

        return $information;
    }

}
